/**
 * 
 */
package cn.com.fig.service;

import java.util.List;
import java.util.Map;

import cn.com.fig.dao.TradeSysMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.com.fig.common.PaginationReqParam;
import cn.com.fig.common.PaginationResponse;
import cn.com.fig.common.ReplyMsg;
import cn.com.fig.common.UuidGen;

/**
 * @author 刘文成
 *被接入的系统
 */
@Service
public class TradeSysService {
	@Autowired
	private TradeSysMapper dao;
	public PaginationResponse<Map<String,Object>> listTradeSys(PaginationReqParam reqParam){
		if(reqParam.isNeedSplitPage())
			PageHelper.startPage(reqParam.getStartIndex(), reqParam.getRows());
		
		List<Map<String,Object>> list = dao.listTradeSys();
		PageInfo<Map<String, Object>> page = new PageInfo<Map<String, Object>>(list);
		
		PaginationResponse<Map<String,Object>> res = new PaginationResponse<Map<String,Object>>();
		res.setNumPerPage(reqParam.getRows());
		res.setRows(list);
		res.setTotalRows(page.getTotal());
		return res;
	}

	/**
	 * 根据sysId判断是否存在主键，若有主键则更新，否则新增
	 * @param tradeSys
	 * @return
	 */
//	@Transactional
	public ReplyMsg save(Map<String,Object> tradeSys){
		int ret = 0;
		String sysId = tradeSys.get("sysId") == null ?null : tradeSys.get("sysId").toString();
		
		if(null == sysId || "".equals(sysId.toString().trim())){
			sysId = UuidGen.genUUID();
			tradeSys.put("sysId", sysId);
			ret = dao.insert(tradeSys);
		}else{
			ret = dao.update(tradeSys);
		}
		ReplyMsg msg = new ReplyMsg();
		if(ret <1){
			msg.setRetCode("-1");
			msg.setRetMsg("保存业务系统数据失败！");
		}
		return msg;
	}
	
	/**
	 * 删除业务系统数据
	 * @param sysId
	 * @return
	 */
	public ReplyMsg del(String sysId){
		int ret = 0;
		ReplyMsg msg = new ReplyMsg();
		
		if(sysId == null || "".equals(sysId)){
			msg.setRetCode("-1");
			msg.setRetMsg("请选择要删除的业务系统");
			return msg;
		}
		
		ret = dao.delete(sysId);
		if(ret <1){
			msg.setRetCode("-1");
			msg.setRetMsg("删除业务系统数据失败！");
		}
		return msg;
	}
}
